import { platForms } from './auth';

//存储桥
let bridge = {
    messageCallBack: {} //内部透传事件列表
}

//原生插件库是否加载完成
let WebViewJavascriptBridgeReady = false;

//用于存储事件桥未加载完成之前的事件列表
let handlerMap = [];

// ios
function setupWebViewJavascriptBridge(cb) {
    if (window.WebViewJavascriptBridge) {
        return callback(window.WebViewJavascriptBridge)
    }

    // 第一次调用的时候，为false
    if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback)
    }

    // 把callback对象赋值给对象
    window.WVJBCallbacks = [callback];

    // 加载WebViewJavascriptBridge_JS中的代码
    var WVJBIframe = document.createElement('iframe')
    WVJBIframe.style.display = 'none'
    WVJBIframe.src = 'https://__bridge_loaded__'
    document.documentElement.appendChild(WVJBIframe)
    setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
    }, 0)
}

bridge.callHandler = function (name, data, callback) {
    if (platForms.isIOS()) {
        setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler(name, data, function (resData) {
                if (!!resData && typeof resData != 'object') {
                    resData = JSON.parse(resData)
                }
                callback(resData)
            })
        })
    } else {
        if (WebViewJavascriptBridgeReady) {
            window.WebViewJavascriptBridge.callHandler(name, data, function (resData) {
                if (!!resData && typeof resData != 'object') {
                    resData = JSON.parse(resData)
                }
                callback(resData)
            })
        } else {
            console.log('2======' + new Date().getTime());
            handlerMap.push({
                type: 'callHandler',
                name: name,
                data: data,
                callback: callback
            })
        }
    }
}

bridge.registerHandler = function (name, callback) {
    if (platForms.isIOS()) {
        setupWebViewJavascriptBridge(function (bridge) {
            bridge.registerHandler(name, function (data, responseCallback) {
                if (!!data && typeof data != 'object') {
                    data = JSON.parse(data)
                }
                callback(data, responseCallback)
            })
        })
    } else {
        if (WebViewJavascriptBridgeReady) {
            window.WebViewJavascriptBridge.registerHandler(name, function (resData, responseCallback) {
                if (!!resData && typeof resData != 'object') {
                    resData = JSON.parse(resData)
                }
                callback(resData, responseCallback)
            })
        } else {
            handlerMap.push({
                type: 'registerHandler',
                name: name,
                callback: callback
            })
        }
    }
}

//判断原生桥函数是否加载完成
let initJsBridge = function () {
    WebViewJavascriptBridgeReady = true
    window.WebViewJavascriptBridge.init(function (message, responseCallback) {
        if (typeof message != 'object') {
            message = JSON.parse(message)
        }
        if (!!message.call && bridge.messageCallBack[message.call]) {
            return responseCallback(bridge.messageCallBack[message.call](message) || '')
        }
    })
}

if (window.WebViewJavascriptBridge) {
    initJsBridge(); //初始化内部通知
} else {
    document.addEventListener('WebViewJavascriptBridgeReady', function () {
        initJsBridge() //初始化内部通知
        handlerMap.forEach(function (v) {
            if (v.type == 'registerHandler') {
                bridge.registerHandler(v.name, v.callback)
            } else if (v.type == 'callHandler') {
                bridge.callHandler(v.name, v.data, v.callback)
            } else if (v.type == 'send') {
                bridge.send(v.data, v.callback)
            }
        })
    }, false)
}

export default bridge;